#!/bin/sh

# Prepare and tag a new version, build release tarballs.

set -e
set -u

myself=${0##*/}

usage()
{
	echo >&2 "Usage: $myself <version>"
	exit 2
}

if ! git rev-parse >'/dev/null' 2>&1
then
	echo >&2 'This script can only be executed within the Git repository.'
	exit 2
elif ! [ -e 'rebar.config' ] || ! [ -e "tools/$myself" ]
then
	echo >&2 "Please call this script from the repository's root directory."
	exit 2
elif [ $# -eq 1 ]
then
	if [ "x$1" = 'x-h' ] || [ "x$1" = 'x--help' ]
	then
		usage
	fi
	new_vsn="$1"
else
	usage
fi

export BUILD_DIR='/build'
export REBAR_COLOR='none'

rel_name='eturnal'
rel_url='https://github.com/processone/eturnal/releases/tag'
src_tar="$rel_name-$new_vsn.tar.gz"
old_vsn="$(git tag -l --sort=version:refname '[0-9]*.[0-9]*.[0-9]*' | tail -1)"
rebar_vsn="$(rebar3 --version | tail -1 |
             sed 's/rebar \([0-9a-z.-]\{1,\}\).*/\1/')"

if [ "$new_vsn" = "$old_vsn" ]
then
	echo >&2 "Tag $new_vsn exists already, aborting."
	exit 1
fi

echo "$myself: Creating release $new_vsn (previous version: $old_vsn)."
echo "$myself: Going to use rebar3 $rebar_vsn."
read -p "$myself: Is the CHANGELOG.md file up-to-date? (y/n) [n] " response
response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
if [ "$response" != 'y' ] && [ "$response" != 'yes' ]
then
	echo >&2 "Okay, aborting."
	exit 1
fi

echo "$myself: Editing repository files ..."
sed -i -e "s|^rel_vsn=.*|rel_vsn='$new_vsn'|" 'tools/get-version'
sed -i -e "s|^$old_vsn-r.*|$new_vsn-r0|" 'tools/ctrrel'
sed -i -e "s|$(echo "$old_vsn" | sed 's/\./\\./g')|$new_vsn|g" \
    'README.md' 'doc/INSTALL.md' 'doc/QUICK-TEST.md' 'doc/overview.edoc' \
    'doc/CONTAINER.md'
sed -i -e "s|^rebar_vsn=.*|rebar_vsn='$rebar_vsn'|" \
    'tools/make-source' 'tools/make-binaries'
sed -i -e "s|compare/$old_vsn|compare/$new_vsn|" \
       -e "s|^## \[Unreleased\]$|&\\
\\
## [$new_vsn] - $(date '+%F')|" \
       -e "s|^\[$old_vsn\]: $rel_url/$old_vsn$|[$new_vsn]: $rel_url/$new_vsn\\
&|" 'CHANGELOG.md'

echo "$myself: Committing result ..."
git add 'rebar.config' 'CHANGELOG.md' 'README.md' 'doc/INSTALL.md' \
    'doc/QUICK-TEST.md' 'doc/overview.edoc' 'doc/CONTAINER.md' \
    'tools/ctrrel' 'tools/get-version' 'tools/make-source' \
    'tools/make-binaries'
git commit -m "Release $new_vsn"
git -P show
read -p "$myself: Does the commit look fine? (y/n) [n] " response
response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
if [ "$response" != 'y' ] && [ "$response" != 'yes' ]
then
	echo >&2 "Okay, aborting."
	exit 1
fi
git tag "$new_vsn"

echo "$myself: Generating documentation ..."
rebar3 edoc
echo "$myself: Editing documentation ..."
sed -e 's|stylesheet.css|/stylesheet.css|' \
    -e 's|erlang\.png|/logo.png|' \
    -e 's|erlang logo|ProcessOne logo|' \
    -e 's|http://www.erlang.org|https://www.process-one.net|' \
    -e 's:overview-summary\.html" target="overviewFrame">Overview:/">Home</a>\&nbsp;|\&nbsp;Documentation\&nbsp;|\&nbsp;<a href="/download/">Download:' \
    -e '/http-equiv/a <meta name="viewport" content="width=device-width, initial-scale=1">' \
    -e '/navbar_top/a <hr>' \
    -e '/Generated by EDoc/d' \
    'doc/overview-summary.html' >'index.html'

tools/make-source
tools/make-binaries
tools/make-installers
tools/make-packages 2>&1 | tail -n 1

echo "$myself: If everything looks fine, run tools/publish-release."
